package com.message.client.util;

import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.net.Socket;

public class ClientSocket {

    /**
     * 测试server是否可连接
     *
     * @param host
     * @param port
     * @return
     */
    public Boolean testHost(String host, Integer port) {
        Boolean flag = false;

        if (StringUtils.isBlank(host) || null == port || 0 == port) {
            return false;
        }

        Socket socket = null;
        try {
            socket = new Socket(host, port);
            socket.close();
            flag = true;
        } catch (IOException e) {

            try {
                if (null != socket) {
                    socket.close();
                }
            } catch (Exception ex) {
                System.out.println("client closed failed; " + e.getMessage());
            }
            System.out.println("client connect failed; " + e.getMessage());
            return false;
        }

        return flag;
    }
}
